package com.kuaimacode.kframework.api.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.api.controller.BaseController;
import com.kuaimacode.kframework.api.service.UserCouponService;
import com.kuaimacode.kframework.util.DictUtil;
import com.kuaimacode.kframework.api.vo.response.CouponResp;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.coupon.UserCoupon;
import io.swagger.annotations.*;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/coupon")
@Api(tags = "用户优惠券",description = "用户优惠券相关api")
public class UserCouponRestController extends BaseController {

    @Autowired
    private UserCouponService userCouponService;

    @ApiOperation(value = "查询用户未使用优惠券", notes="查询用户未使用优惠券")
    @GetMapping("/not_used")
    public JsonResult<CouponResp> queryNotUsedCoupons(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
                                                      @RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize)
    {

        Long userId = getUserId();

        Integer count = userCouponService.queryStatusCount(userId, Constant.COUPON_STATUS_NOT_USED);
        PageInfo<UserCoupon> pageInfo = userCouponService.queryByState(pageNum,pageSize,userId, Constant.COUPON_STATUS_NOT_USED);

        CouponResp couponResp = new CouponResp();
        couponResp.setNot_used_count(count);

        List<UserCoupon> coupons = pageInfo.getList();

        for(UserCoupon coupon :coupons){

            coupon.setUseScopeDesc(getScope(coupon));
        }

        pageInfo.setList(coupons);

        couponResp.setNot_used_coupons(pageInfo);
        return JsonResult.success(couponResp);
    }


    @ApiOperation(value = "查询订单可使用的优惠券使用优惠券", notes="查询订单可使用的优惠券使用优惠券")
    @GetMapping("/order_coupons")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderType", value = "订单大类", required = true, dataType = "string"),
            @ApiImplicitParam(name = "childType", value = "订单小类", required = true, dataType = "string"),
            @ApiImplicitParam(name = "amount", value = "订单金额", required = true, dataType = "double")

    })
    public JsonResult<UserCoupon> queryOrderCoupons(String orderType,String childType,BigDecimal amount)
    {
        Long userId = getUserId();
        List<UserCoupon> coupons = userCouponService.queryOrderCoupon(userId, orderType,childType,amount);


        for(UserCoupon coupon :coupons){

            coupon.setUseScopeDesc(getScope(coupon));
        }

        return JsonResult.success(coupons);
    }


    public String getScope(UserCoupon coupon){
        if(coupon.getUseScope()==null ||"".equals(coupon.getUseScope())){
            return "通用";
        }else{
            String serviceClass = DictUtil.keyValue("SERVICE_CLASS",coupon.getUseScope());
            String childType = coupon.getUseScopeChildType();
            if(childType!=null && !"".equals(childType)){
                String subClass = DictUtil.keyValueByPCode("SERVICE_CLASS"+coupon.getUseScope(),coupon.getUseScope(),childType);
                return serviceClass + " "+ subClass;
            }
            return serviceClass;
        }

    }


    @ApiOperation(value = "查询用户已使用优惠券", notes="查询用户已使用优惠券")
    @GetMapping("/used")
    public JsonResult queryUsedCoupons(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
                                       @RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize){

        Long userId = getUserId();

        Integer count = userCouponService.queryStatusCount(userId, Constant.COUPON_STATUS_USED);
        PageInfo<UserCoupon> pageInfo = userCouponService.queryByState(pageNum,pageSize,userId, Constant.COUPON_STATUS_USED);
        CouponResp couponResp = new CouponResp();

        List<UserCoupon> coupons = pageInfo.getList();

        for(UserCoupon coupon :coupons){

            coupon.setUseScopeDesc(getScope(coupon));
        }

        pageInfo.setList(coupons);

        couponResp.setUsed_count(count);
        couponResp.setUsed_coupons(pageInfo);
        return JsonResult.success(couponResp);
    }


    @ApiOperation(value = "查询用户已过期优惠券", notes="查询用户已过期优惠券")
    @GetMapping("/expire")
    public JsonResult queryExpireCoupons(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
                                         @RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize){

        Long userId = getUserId();

        Integer count = userCouponService.queryStatusCount(userId, Constant.COUPON_STATUS_EXPIRE);
        PageInfo<UserCoupon> pageInfo = userCouponService.queryByState(pageNum,pageSize,userId, Constant.COUPON_STATUS_EXPIRE);
        CouponResp couponResp = new CouponResp();

        List<UserCoupon> coupons = pageInfo.getList();

        for(UserCoupon coupon :coupons){

            coupon.setUseScopeDesc(getScope(coupon));
        }

        pageInfo.setList(coupons);

        couponResp.setExpire_count(count);
        couponResp.setExpire_coupons(pageInfo);
        return JsonResult.success(couponResp);
    }
}
